home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0921.ZIP / JOYSTK.ARC / JOYSTICK.DOC < prev    next >
Text File  |  1987-03-29  |  3KB  |  68 lines

  1.                    TURBO PASCAL JOYSTICK ROUTINES
  2.  
  3.         The IBM joystick port is located at hex 201.  All the information 
  4. about both joysticks can be read in the byte at that port.  Each bit reports 
  5. on a specific thing, namely:
  6.  
  7.            Bit                 Reports on
  8.  
  9.             0            Joystick A X-Axis Coordinate
  10.             1            Joystick A Y-Axis Coordinate
  11.             2            Joystick B X-Axis Coordinate
  12.             3            Joystick B Y-Axis Coordinate
  13.             4            Joystick A Button #1 (this is usually the top button)
  14.             5            Joystick A Button #2
  15.             6            Joystick B Button #1
  16.             7            Joystick B Button #2
  17.  
  18.         To read the status of a button, you simply read the relevant bit.  If 
  19. the bit is 1, the button is not pressed; if it is 0, the button is pressed.  
  20. Aside from the fact that this relationship seems counterintuitive, reading the 
  21. buttons is a simple task, easily accomplished with the Turbo Pascal port array 
  22. and a little bit masking.
  23.  
  24.         The stick itself is a different story.  How is it possible to get 
  25. enough information from one bit?  The answer is that you get it by repeated 
  26. reads of the relevant bit.  The process works like this:  when you output 
  27. something--anything--to port 201, the lower four bits are all set to 1 
  28. automatically.  (I think of this as "nudging" the port.)  The length of time 
  29. it takes each bit to return to 0 depends on the position of the stick.  For 
  30. example, if Joystick A is all the way to the left, Bit 0 will bounce back to 0 
  31. immediately after a nudge.  If it is all the way to the right, it will take 
  32. longer to return to 0.  If it is centered, it will take about half as long to 
  33. return to 0 as it does when it is all the way to the right.
  34.  
  35.         You can use the Turbo Pascal port array in a loop to make successive 
  36. reads of port 201, but that's very slow.  The successive reads are too far 
  37. apart to be very reliable as a direction indicator, and they are woefully 
  38. inadequate to use as a position indicator.  (If you don't understand the 
  39. distinction between a direction indicator and a position indicator, compare 
  40. JOYDEMO2.PAS with JOYDEMO3.PAS.)
  41.  
  42.         Only machine code is fast enough to get a good reading of the joystick 
  43. port.  And, of course, Turbo Pascal makes that easy (at least as easy as it 
  44. ever can be) with the inline procedure.  READJOY.INC is an inline procedure 
  45. written by me to do the job.  I hope you find it useful.
  46.  
  47.         The ARC file that includes this file comprises seven others.  This may 
  48. be a bit of overkill, but I thought some might find it helpful to have as much 
  49. information as possible.  The files have comments where I thought a bit of 
  50. exegesis might be in order.  The eight files are: 
  51.  
  52.         JOYSTICK.DOC    This file.
  53.         READJOY.INC     The key file, with the inline procedure.
  54.         BUTTONS.INC     Functions to read the joystick buttons.
  55.         JOYPRSNT.INC    Function to determine whether a joystick is installed.
  56.         JOYDEMO1.PAS
  57.         JOYDEMO2.PAS
  58.         JOYDEMO3.PAS
  59.         JOYDEMO4.PAS
  60.  
  61.         I would appreciate any comments or suggestions.
  62.  
  63.                                                 David Howorth
  64.                                                 CIS User ID: 71600,521
  65.  
  66.                                                 March 29,1987
  67.  
  68.